/*
 * The MIT License
 *
 * Copyright 2012 Anonymous.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package pentomino;

import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import pentomino.ui.PentominoFrame;


/**
 * 
 * A general class of the application. 
 */
public class Program {
    private static HashMap<String, Long> highscores;
    private static Preferences userPrefs;
    
    public static void addNewHighscore(String user, long value)
    {
        if (highscores != null)
        {
            if (user == null || user.isEmpty())
                user = "Nobody";
            
            highscores.put(user, value);
        }
    }
    
    public static HashMap<String, Long> getHighscores()
    {
        return highscores;
    }
    
    public static void saveSettings()
    {
        if (highscores != null && userPrefs != null)
        {
            try {
                userPrefs.clear();
                
                for (String key : highscores.keySet())
                {
                    userPrefs.putLong(key, highscores.get(key));
                }
            } catch (BackingStoreException ex) {
                Logger.getLogger(Program.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    /**
     * The main entry point for the application.
     * 
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        userPrefs = Preferences.userNodeForPackage(Program.class);
        highscores = new HashMap<String, Long>();
               
        try {
            String[] users = userPrefs.keys();
            
            for (int i = 0; i < users.length; i++)
            {
                highscores.put(users[i], userPrefs.getLong(users[i], 0L));
            }
            
        } catch (BackingStoreException ex) {
            Logger.getLogger(Program.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        PentominoFrame frame = new PentominoFrame();
        frame.setVisible(true);
    }
}
